package com.demo.apparel.service.web.Impl;

import com.demo.apparel.dto.web.AdminFeedbackDTO;
import com.demo.apparel.dto.ReplyFeedbackDTO;
import com.demo.apparel.entity.FeedbackImg;
import com.demo.apparel.mapper.web.AdminFeedbackMapper;
import com.demo.apparel.mapper.uniapp.WxFeedbackImgMapper;
import com.demo.apparel.service.web.FeedbackService;
import com.demo.apparel.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FeedbackServiceImpl implements FeedbackService {

    @Autowired
    private AdminFeedbackMapper adminFeedbackMapper;

    @Autowired
    private WxFeedbackImgMapper wxFeedbackImgMapper;


    /**
     *
     * @param status
     * @param contact
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageVO<AdminFeedbackDTO> getFeedbackPage(Integer status, String contact, int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        List<AdminFeedbackDTO> list;
        long total;

        boolean hasContact = contact != null && !contact.trim().isEmpty();

        if (status != null && hasContact) {
            list = adminFeedbackMapper.selectByStatusAndContact(status, contact, offset, pageSize);
            total = adminFeedbackMapper.countByStatusAndContact(status, contact);
        } else if (status != null) {
            list = adminFeedbackMapper.selectByStatus(status, offset, pageSize);
            total = adminFeedbackMapper.countByStatus(status);
        } else if (hasContact) {
            list = adminFeedbackMapper.selectByContact(contact, offset, pageSize);
            total = adminFeedbackMapper.countByContact(contact);
        } else {
            list = adminFeedbackMapper.selectAll(offset, pageSize);
            total = adminFeedbackMapper.countAll();
        }

        for (AdminFeedbackDTO dto : list) {
            List<FeedbackImg> imgs = wxFeedbackImgMapper.selectByFeedbackId(dto.getId());
            List<String> urls = imgs.stream().map(FeedbackImg::getImgUrl).toList();
            dto.setImageUrls(urls);
        }

        return new PageVO<>(list, total, pageNum, pageSize);
    }

    /**
     *
     * @param dto
     */
    @Override
    public void replyFeedback(ReplyFeedbackDTO dto) {
        int updated = adminFeedbackMapper.updateResponse(
                dto.getFeedbackId(),
                dto.getAdminId(),
                dto.getResponse(),
                dto.getStatus() != null ? dto.getStatus() : 2 // 默认设为2: 已处理
        );
        if (updated == 0) {
            throw new RuntimeException("反馈不存在或回复失败");
        }
    }


    @Override
    public void deleteFeedback(String feedbackId) {
        int deleted = adminFeedbackMapper.deleteById(feedbackId);
        if (deleted == 0) {
            throw new RuntimeException("反馈不存在或删除失败");
        }
    }

    @Override
    public void deleteFeedbackBatch(List<String> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new RuntimeException("删除失败，id不能为空");
        }
        int deleted = adminFeedbackMapper.deleteBatch(ids);
        if (deleted == 0) {
            throw new RuntimeException("删除失败，可能记录不存在");
        }
    }




}
